<?php
/**
 * Created by PhpStorm
 */
namespace app\admin\controller;
use think\Db;
use think\Request;
class Jindu extends Base {

    /**
     *
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax()){
            extract(input());
            $user_name = trim(input('user_name'), ' ');
            $where = [];
            if(isset($user_name) && $user_name != ''){
                $where['jindu_name'] = ['like', '%' . $user_name . '%'];
            }

            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::table('up_jindu')
                ->where($where)
                ->count();// 获取总条数
            $lists = Db::table('up_jindu')
                ->where($where)
                ->order('jindu_sort desc,jindu_time asc')
                ->page($NowPage,$limits)
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }
    /**
     * 添加
     * @return mixed|\think\response\Json
     */
    public function typeAdd(){
        if(Request::instance()->isPost()){
            extract(input());
            $data = [
                'jindu_name' => $jindu_name,
                'jindu_sort' => $jindu_sort,
                'jindu_time' => date('Y-m-d H:i:s'),
            ];
            $res = Db::table('up_jindu')->insert($data);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        return $this->fetch('jindu/typeadd');
    }
    /**
     * 编辑
     * @return mixed
     */
    public function typeEdit(){
        if(Request::instance()->isPost()){
            extract(input());
            $data = [
                'jindu_name' => $jindu_name,
                'jindu_sort' => $jindu_sort,
                'jindu_time' => date('Y-m-d H:i:s'),
            ];
            $res = Db::table('up_jindu')->where('jindu_id',$jindu_id)->update($data);
            if($res){
                return json(['code' => 200, 'msg' => '成功']);
            }else{
                return json(['code' => 100, 'msg' => '失败']);
            }
        }
        $jindu_id = input('jindu_id');
        $info = Db::name('jindu')
            ->where('jindu_id',$jindu_id)
            ->find();
        $this->assign([
            'info' => $info,
        ]);
        return $this->fetch('jindu/typeedit');
    }
    /**
     * 排序编辑
     * @return mixed|\think\response\Json
     */
    public function updateSort(){
        $id = input('id');
        $param = [
            'jindu_sort'=>input('value'),
        ];
        $res = Db::name('jindu')->where('jindu_id',$id)->update($param);
        if($res){
            return json(['code' => 200, 'msg' => '修改成功']);
        }else{
            return json(['code' => 100, 'msg' =>' 修改失败']);
        }
    }
    /**
     * 删除
     * @return \think\response\Json
     */
    public function typeDel(){
        $nt_id = input('id');
        $res = Db::table('up_jindu')->where('jindu_id',$nt_id)->delete();
        if($res){
            return json(['code' => 200, 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }
}